<?php

// Installed?
if (filesize('config.php') == 0) { header('Location: install/index.php'); exit; }

define('APP','CATALOG');

// Include Config and Common
require('config.php');
require('common.php');

// Locator
require(DIR_LIBRARY . 'locator.php');
$locator =& new Locator();

// Config
$config =& $locator->get('config');

// Database
$database =& $locator->get('database');
$database->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

// Settings
$sql="select * from setting where type = 'catalog' or type = 'global'";
$settings = $database->getRows($sql);

foreach ($settings as $setting) {
	$config->set($setting['key'], $setting['value']);
}

// Image
$image =& $locator->get('image');

// Request
$request =& $locator->get('request');

// URL
$url =& $locator->get('url');

// Language
$language =& $locator->get('language');

// Base URL
$catalog_url = $request->isSecure()?HTTPS_SERVER:HTTP_SERVER;
$image_url = $request->isSecure()?HTTPS_IMAGE:HTTP_IMAGE;

// Product Data
$product_data = array();
$sql="select *, p.date_added as date_product_added from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.status = '1' and pd.language_id = '%s' and p.date_available < now() and p.status = '1' order by date_product_added desc limit 20";
$sql=sprintf($sql,(int)$language->getId());
$results = $database->getRows($sql);
$unit = 'Pounds';
$products=array();
foreach ($results as $result) {
	$products[]=array(
	'name' => htmlentities(strip_tags($result['name'])),
	'url' => $url->href('product', FALSE, array('product_id' => $result['product_id'])),
	'add_date' => date("D, d M Y H:i:s T", strtotime($result['date_product_added'])),
	//'desc' => htmlentities(strip_tags($result['description'],'ENT_QUOTES')),
	'desc' => htmlentities('<img width="100" height="100" src="' . $image->resize($result['filename'], 100, 100) . '" /><br />') .
			  htmlentities(strip_tags($result['description'],'ENT_QUOTES')),
	'thumb' => $image->resize($result['filename'], 100, 100),
	'id' => $result['product_id'],
	'price' => $result['price'],
	'model' => $result['model'],
	'weight' => $result['weight'] . ' ' . $unit
	);
}

header('Content-type: text/xml');
echo '<?xml version="1.0"?>';
?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
<channel>
	<title><?php echo $config->get('config_store'); ?></title>
	<description><?php echo $config->get('config_store'); ?></description>
	<link><?php echo $catalog_url; ?></link>
	<copyright><?php echo $config->get('config_store'); ?></copyright>
<?php foreach ($products as $product) { ?>
	<item>
        <title><?php echo $product['name']; ?></title>
        <g:brand></g:brand>
		<g:condition>new</g:condition>
		<g:product_type></g:product_type>
        <description><?php echo $product['desc']; ?></description>
        <link><?php echo $product['url']; ?></link>
        <pubDate><?php echo $product['add_date']; ?></pubDate>
        <guid isPermaLink="true"><?php echo $product['url']; ?></guid>
        <g:id><?php echo $product['id']; ?></g:id>
        <g:image_link><?php echo $product['thumb']; ?></g:image_link>
		<!--g:expiration_date>2010-01-01</g:expiration_date-->
		<g:price><?php echo $product['price']; ?></g:price>
		<g:mpn><?php echo $product['model']; ?></g:mpn>
		<g:weight><?php echo $product['weight']; ?></g:weight>
		<g:currency><?php echo $config->get('config_currency'); ?></g:currency>
	</item>
<?php } ?>
</channel>
</rss>